using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Plugin.Sns.Scene;
using MicroMsg.Resource.@string;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace MicroMsg.UI.UserContrl
{
	public class NewsSessionItem : UserControl, MMItemCtrl, BaseHeadImgItem
	{
		public delegate void ListItemClick(object sender, object object_0);
		private const string TAG = "NewsSessionItem";
		private const int TYPE_NORMAL = 0;
		private const int TYPE_UNREAD_VOICE = 1;
		public static DependencyProperty ListItemProperty = DependencyProperty.Register("ItemObject", typeof(TXNews), typeof(NewsSessionItem), new PropertyMetadata(null, new PropertyChangedCallback(NewsSessionItem.OnItemPropertyChanged)));
		private EventWatcher sWatcher;
		private long ShowTimer = -1L;
		private static Brush[] infoBrush = new Brush[]
		{
			R.theme.LISTA_SIGNATURE_WORD_BRUSH,
			R.theme.LISTA_SIGNATURE_UNREAD_WORD_BRUSH
		};
		public static HeadImgLoadService mHeadImgLoadService = new HeadImgLoadService(true);
		private bool isHeadImgVisible;
		internal TextBlock TimeTxt2;
		internal StackPanel NewsContent;
		internal MenuItem DeleteMsgMenu;
		internal TextBlock WaitTxt;
		internal TextBlock TitleTxt;
		internal Image HeadImg;
		internal TextBlock InfoPre;
		internal TextBlock TimeTxt;
		private bool _contentLoaded;
		public event NewsSessionItem.ListItemClick ItemClick;
		public TXNews ItemObject
		{
			get
			{
				return (TXNews)base.GetValue(NewsSessionItem.ListItemProperty);
			}
			set
			{
				base.SetValue(NewsSessionItem.ListItemProperty, value);
			}
		}
		public string UserName
		{
			get
			{
				return "";
			}
			set
			{
			}
		}
		public NewsSessionItem()
		{
			base.add_Loaded(new RoutedEventHandler(this.NewsSessionItem_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.NewsSessionItem_Unloaded));
			this.InitializeComponent();
			this.InitBindResource();
		}
		private void NewsSessionItem_Loaded(object sender, RoutedEventArgs e)
		{
			NewsSessionItem.mHeadImgLoadService.Add(this);
			this.RefreshTimerTxt();
			base.SetValue(TiltEffect.IsTiltEnabledProperty, true);
		}
		private void NewsSessionItem_Unloaded(object sender, RoutedEventArgs e)
		{
			NewsSessionItem.mHeadImgLoadService.Delete(this);
		}
		private void InitBindResource()
		{
			this.DeleteMsgMenu.Header = strings.COM_Del;
			this.DeleteMsgMenu.set_Foreground(R.theme.CONTEXTMENU_FOREGROUND_BRUSH);
			this.TitleTxt.set_FontFamily((FontFamily)Application.get_Current().get_Resources().get_Item("PhoneFontFamilySemiLight"));
			this.TimeTxt2.set_Foreground(R.theme.LISTA_NAME_WORD_BRUSH);
			this.InfoPre.set_FontFamily((FontFamily)Application.get_Current().get_Resources().get_Item("PhoneFontFamilySemiLight"));
			this.TimeTxt.set_FontFamily((FontFamily)Application.get_Current().get_Resources().get_Item("PhoneFontFamilySemiLight"));
		}
		private void Button_Click(object sender, RoutedEventArgs e)
		{
			if (this.ItemClick != null)
			{
				this.ItemClick(this, this.ItemObject);
			}
		}
		private static void OnItemPropertyChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_Property() == NewsSessionItem.ListItemProperty)
			{
				(dependencyObject_0 as NewsSessionItem).OnListItemPropertyChanged(dependencyPropertyChangedEventArgs_0);
			}
		}
		private void OnListItemPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			try
			{
				this.RefreshItem(dependencyPropertyChangedEventArgs_0.get_NewValue() as TXNews);
			}
			catch (Exception ex)
			{
				Log.smethod_1(base.GetType().get_Name(), ex.get_StackTrace());
			}
		}
		private void RefreshItem(TXNews item)
		{
			if (item == null)
			{
				return;
			}
			this.TitleTxt.set_Text(item.strTitle);
			this.TimeTxt.set_Text(Util.formatNewsTimeInList(item.nPubTime, true));
			this.TimeTxt2.set_Text(Util.formatTimeInChatMsg(item.nPubTime));
			this.ShowTimer = item.nPubTime;
			this.InfoPre.set_Text(item.strDescipt);
			if (!string.IsNullOrEmpty(item.strCoverImgUrl))
			{
				snsMediaDownloadInfo newsImg = NewsMgr.getNewsImg(item.strCoverImgUrl);
				if (newsImg.object_0 != null)
				{
					this.HeadImg.set_Source(ImageCacheMgr.loadFromBuf(SnsMediaStorage.load(item.strCoverImgUrl)));
					return;
				}
				if (this.sWatcher == null)
				{
					this.sWatcher = new EventWatcher(this, null, new EventHandlerDelegate(this.OnEvent));
					EventCenter.registerEventWatcher(EventConst.ON_MGR_SNS_GET_MEDIA_FINISH, this.sWatcher);
					return;
				}
			}
			else
			{
				this.HeadImg.set_Visibility(1);
			}
		}
		private void OnEvent(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			if (evtArgs.mEventID == EventConst.ON_MGR_SNS_GET_MEDIA_FINISH)
			{
				if (evtArgs.mObject == null)
				{
					return;
				}
				if (evtArgs.mObject is List<snsMediaDownloadInfo>)
				{
					return;
				}
				if (evtArgs.mObject is snsMediaDownloadInfo)
				{
					snsMediaDownloadInfo snsMediaDownloadInfo = evtArgs.mObject as snsMediaDownloadInfo;
					if (snsMediaDownloadInfo.snsObjID == 1uL)
					{
						EventCenter.removeEventWatcher(EventConst.ON_MGR_SNS_GET_MEDIA_FINISH, this.sWatcher);
						this.sWatcher = null;
						if (snsMediaDownloadInfo.object_0 != null)
						{
							this.HeadImg.set_Source((BitmapImage)snsMediaDownloadInfo.object_0);
						}
					}
				}
			}
		}
		public void RefreshTimerTxt()
		{
			if (this.ShowTimer < 0L)
			{
				return;
			}
			this.TimeTxt.set_Text(Util.formatNewsTimeInList(this.ShowTimer, true));
			this.TimeTxt2.set_Text(Util.formatTimeInChatMsg(this.ShowTimer));
		}
		private int getInfoShowColorType(TXNews item)
		{
			if (item == null)
			{
				return 0;
			}
			return 0;
		}
		public void HeadImgLoaded()
		{
			this.isHeadImgVisible = true;
		}
		public void SetVisible()
		{
			if (this.isHeadImgVisible)
			{
				return;
			}
			this.isHeadImgVisible = true;
		}
		public void ScreenIn()
		{
		}
		public void ScreenOut()
		{
		}
		public void Refresh()
		{
			this.RefreshItem(this.ItemObject);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/TXNews/UI/NewsSessionItem.xaml", 2));
			this.TimeTxt2 = (TextBlock)base.FindName("TimeTxt2");
			this.NewsContent = (StackPanel)base.FindName("NewsContent");
			this.DeleteMsgMenu = (MenuItem)base.FindName("DeleteMsgMenu");
			this.WaitTxt = (TextBlock)base.FindName("WaitTxt");
			this.TitleTxt = (TextBlock)base.FindName("TitleTxt");
			this.HeadImg = (Image)base.FindName("HeadImg");
			this.InfoPre = (TextBlock)base.FindName("InfoPre");
			this.TimeTxt = (TextBlock)base.FindName("TimeTxt");
		}
	}
}
